# -*- coding: utf-8 -*-
#coding=utf-8
import yaml, requests,json, pymysql
from data.data_154 import *
from data.data_uat import *

def write_yaml(path,data):
    with open(path,'w',encoding='utf-8')as file:
        yaml.dump(data=data,stream=file,allow_unicode=True)
        print('接口数据写入成功！')
def read_yaml(num,name):
    with open(r'D:\python\dekeinfo_Management\data\add_employee_requests.yaml', 'r', encoding='utf-8')as file1:
        data=yaml.load(file1,Loader=yaml.FullLoader)
        return data[num][name]

# write_yaml(r'D:\python\dekeinfo_Management\data\add_employee_requests.yaml', data1)

def send_requests(num,name,url,data,headers):
    if read_yaml(num,name)['method']=='post':
        re = requests.post(url, data=json.dumps(data), headers=headers)
        return  re
    if read_yaml(num,name)['method']=='get':
        re=requests.get(url, params=data,headers=headers)
        return  re
def mysql(base):
    #154数据库
    if base=='154':
        mysql_host = '172.16.91.155'
        mysql_port = 3306
        mysql_user = 'root'
        mysql_passwd = 'deke@2024'
        mysql_database = 'dekeinfo_154'
    if base=='uat':
    #UAT数据库
        mysql_host = 'test.derkee.com'
        mysql_port = 3306
        mysql_user = 'root'
        mysql_passwd = 'uat@dekeinf.mysql'
        mysql_database = 'uat'
    conn= pymysql.connect(host=mysql_host,port=mysql_port, user=mysql_user, passwd=mysql_passwd, charset='utf8', db=mysql_database)
    cursor=conn.cursor()
    return conn

#员工重要信息完整度定时任务
def task_():
    url = read_yaml(12, '触发员工重要信息完整度定时任务')['url']
    headers = read_yaml(12, '触发员工重要信息完整度定时任务')['headers']
    data = read_yaml(12, '触发员工重要信息完整度定时任务')['data']
    expect = read_yaml(12, '触发员工重要信息完整度定时任务')['expect']
    ree = send_requests(12, '触发员工重要信息完整度定时任务', url, data=data, headers=headers)




def login():
    '''
    这是网页登录
    '''
    url = read_yaml(0, '登录')['url']
    headers = read_yaml(0, '登录')['headers']
    data1 = read_yaml(0, '登录')['data']
    expect = read_yaml(0, '登录')['expect']
    ree = send_requests(0, '登录', url, data=data1, headers=headers)
    a1 = json.loads(ree.text)
    token = a1['data']['token']
    return token

def mini_login(username):
    """
    小程序登录
    """
    url='http://172.16.91.154/new/oa_wechat/mini/login'
    headers={'Content-Type':'application/json'}
    data={"userNumber":username,"password":"NOkmmLIICPOUowfZi9qCnDVjg2YeUloswalUxhpprcz8CKAe33PrGPpwZkOiEsHoqfK/vABqwYyp2j4dMvR4pCuulWH0ng80ZQX5C2bZrIAcqjA9QEo1WGhzlYCDDgwrrTjnVIvkbVrJxH3mWbD4dvtDS/niR9pgASh6vocRTL+FuygWBjo79OMgPLJiM7eLPtfxm1wP7wv7DjHVOWWnyadven2/cpYA1IHhwAKyDuGDWreYFSPTe/48icrJzICZxAyDlSPrJR6R5krkmHHqu1eqD7D/99LKCslKFBzdGTpxFNgd202shsvBzkCEwjpkQL3gLHKsx152pLKWkFw4dA=="}
    ree = requests.post( url, data=json.dumps(data), headers=headers)
    a1 = json.loads(ree.text)
    token = a1['data']
    return token

#审批流程
def audit_check(audit_id,cursor,conn,token):
    for i in range(6):
        # 查询接口
        url1 = read_yaml(4, '查询流程')['url']
        headers1 = read_yaml(4, '查询流程')['headers']
        headers1['Authorization'] = token
        data_1 = read_yaml(4, '查询流程')['data']
        data_1['auditId'] = audit_id
        expect1 = read_yaml(4, '查询流程')['expect']
        ree = send_requests(4, '查询流程', url=url1, data=data_1, headers=headers1)
        a1 = json.loads(ree.text)
        # self.assertEqual(a1['status'], expect1)
        auditNodeId = a1['data']['auditNodeId']
        auditOrder = a1['data']['auditOrder']
        # 审批接口
        url2 = read_yaml(5, '审批流程')['url']
        headers2 = read_yaml(5, '审批流程')['headers']
        headers2['Authorization'] = token
        expect2 = read_yaml(5, '审批流程')['expect']
        data2 = read_yaml(5, '审批流程')['data']
        data2['auditId'] = audit_id
        data2['auditNodeId'] = auditNodeId
        data2['auditOrder'] = auditOrder
        ree1 = send_requests(5, '审批流程', url=url2, data=data2, headers=headers2)
        a2 = json.loads(ree1.text)
        # self.assertEqual(a2['status'], expect2)
        # 查询流程状态
        sql6 = "SELECT audit_status FROM audit WHERE audit_id=%s" % (audit_id)
        cursor.execute(sql6)
        conn.commit()
        result = cursor.fetchall()
        for i in result:
            audit_status = i[0]
        print(audit_status)
        if audit_status == 0:
            print("流程没审批完，请继续！")
            continue
        if audit_status == 1:
            print('流程审核通过啦！')
            break

#查询员工信息完整度实时风险列表数据（按员工工号筛选）
def missingInfoName(idd):
        token = login()
        url = read_yaml(9, '查询员工重要信息完整度实时列表页')['url']
        headers = read_yaml(9, '查询员工重要信息完整度实时列表页')['headers']
        headers['Authorization'] = token
        expect = read_yaml(9, '查询员工重要信息完整度实时列表页')['expect']
        data = read_yaml(9, '查询员工重要信息完整度实时列表页')['data']
        data['employeeIdOrName']=idd
        ree = send_requests(9, '查询员工重要信息完整度实时列表页', url, data=data, headers=headers)
        a1 = json.loads(ree.text)
        return a1
#查询员工信息完整度历史风险列表数据（按员工工号筛选）
def missingInfoName_history(idd):
    token = login()
    url = read_yaml(10, '查询员工重要信息完整度历史列表页')['url']
    headers = read_yaml(10, '查询员工重要信息完整度历史列表页')['headers']
    headers['Authorization'] = token
    expect = read_yaml(10, '查询员工重要信息完整度历史列表页')['expect']
    data = read_yaml(10, '查询员工重要信息完整度历史列表页')['data']
    data['employeeIdOrName'] = idd
    ree = send_requests(10, '查询员工重要信息完整度历史列表页', url, data=data, headers=headers)
    a1 = json.loads(ree.text)
    return a1